package com.example.studyllm.service.rag;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studyllm.config.exception.BusinessException;
import com.example.studyllm.constants.CommonConstant;
import com.example.studyllm.entity.rag.RagUser;
import com.example.studyllm.mapper.rag.RagUserMapper;
import com.example.studyllm.service.base.BaseServiceImpl;
import com.example.studyllm.utils.BeanUtils;
import com.example.studyllm.vo.base.PageResultVO;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;

/**
 * <p>
 *   service实现类
 * </p>
 *
 * @author kw
 * @since 2025-02-05 15:12:51.349157
 */
@Service
public class RagUserService extends BaseServiceImpl<RagUserMapper, RagUser> {


    public Boolean addRagUser(RagUser ragUser) {
        return this.save(ragUser);
    }

    public Boolean updateRagUserById(RagUser ragUser) {
        return this.updateById(ragUser);
    }

    public PageResultVO<RagUser> getPage(RagUser pageParam) {

        LambdaQueryWrapper<RagUser> lambda = new QueryWrapper<RagUser>().lambda();
        this.buildCondition(lambda, pageParam);
        lambda.orderBy(true, false, RagUser::getId);           
        PageResultVO<RagUser> resultPage = BeanUtils.changePage(RagUser.class, this.page(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), lambda));
        return resultPage;
    }
    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<RagUser> lambda, RagUser param) {
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getId()),RagUser::getId, param.getId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getName()),RagUser::getName, param.getName());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getPassword()),RagUser::getPassword, param.getPassword());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getPhone()),RagUser::getPhone, param.getPhone());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCover()),RagUser::getCover, param.getCover());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getStatus()),RagUser::getStatus, param.getStatus());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCtime()),RagUser::getCtime, param.getCtime());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUtime()),RagUser::getUtime, param.getUtime());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getDeleteFlag()),RagUser::getDeleteFlag, param.getDeleteFlag());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCby()),RagUser::getCby, param.getCby());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUby()),RagUser::getUby, param.getUby());
        
    }

    public RagUser login(RagUser ragUser) {
        RagUser user = lambdaQuery().eq(RagUser::getPhone,ragUser.getPhone()).one();
        if(user==null || !ObjectUtil.equal(user.getPassword(), ragUser.getPassword())){
            throw new BusinessException("用户名或密码错误");
        }
        if(user.getStatus().equals(CommonConstant.STATUS_OFF_FLAG)){
            throw new BusinessException("用户已被禁用");
        }
        return user;
    }
}